﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Common;
using MesProjectWinform.Production.Dto.Input.Packing;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using MesProjectWinform.Public.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.Packing
{
    public partial class AddPackingForm : DevExpress.XtraEditors.XtraForm
    {
        public AddPackingForm()
        {
            InitializeComponent();
        }

        private void AddForm_Load(object sender, EventArgs e)
        {
            #region 初始化客户下拉框
            lookUpEdit2.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="类型1"},
                new TypeModel(){Id=2,Name="类型2"},
                new TypeModel(){Id=2,Name="类型3"},
                new TypeModel(){Id=2,Name="类型4"}
            };
            lookUpEdit2.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit2.Properties.ValueMember = "Name"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit2.Properties.PopulateColumns();
            lookUpEdit2.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit2.Properties.NullText = "请选择客户";
            #endregion
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            if (lookUpEdit2.EditValue == null)
            {
                MessageBox.Show("请选择类型");
                return;
            }
            AddPackingInput input = new AddPackingInput
            {
                PACKINGNAME = textEdit2.Text,
                PACKINGCODE = textEdit1.Text,
                PACKINGTYPE = lookUpEdit2.EditValue, // 仍用界面选择
                INNERBOXLENGTH = spinEdit1.Value, // 使用SpinEdit控件的值
                INNERBOXWIDTH = spinEdit1.Value, // 使用SpinEdit控件的值
                INNERBOXHEIGHT = spinEdit1.Value, // 使用SpinEdit控件的值
                INNERBOXWEIGHTMIN = spinEdit1.Value, // 使用SpinEdit控件的值
                INNERBOXWEIGHTMAX = spinEdit1.Value, // 使用SpinEdit控件的值
                INNERBOXCAPACITY = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXLENGTH = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXWIDTH = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXHEIGHT = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXWEIGHTMIN = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXWEIGHTMAX = spinEdit1.Value, // 使用SpinEdit控件的值
                OUTERBOXCAPACITY = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETLENGTH = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETWIDTH = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETHEIGHT = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETWEIGHTMIN = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETWEIGHTMAX = spinEdit1.Value, // 使用SpinEdit控件的值
                PALLETCAPACITY = spinEdit1.Value, // 使用SpinEdit控件的值
                PACKINGDESCRIBE = memoEdit1.Text // 使用MemoEdit控件的值
            };

            // 这里可以继续你的后续逻辑，比如保存、提交等
            using (var client = new HttpClient())
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(input);
                var response = await HttpClientHelper.RequestUrl(RequestType.Post, ProductionGolab.AddPacking, json);
                var apiResult = JsonConvert.DeserializeObject<ApiResult<int>>(response);
                if (apiResult.Code==ApiEnums.Success)
                {
                    MessageBox.Show($"{apiResult.Message}");
                    this.DialogResult = DialogResult.OK;
                    this.Close(); // 关闭当前窗口
                }
                else
                {
                    MessageBox.Show($"{apiResult.Message}");
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close(); // 关闭当前窗口
        }
    }
}